package fina2.dcs.fi;

import java.io.File;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.naming.InitialContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;
import org.richfaces.component.UITree;
import org.richfaces.component.html.HtmlTree;
import org.richfaces.component.state.TreeState;
import org.richfaces.event.NodeExpandedEvent;
import org.richfaces.model.TreeRowKey;

import fina2.dcs.context.ContextHelper;
import fina2.dcs.fis.Bank;
import fina2.dcs.fis.BankGroup;
import fina2.dcs.fis.BankGroupLocal;
import fina2.dcs.fis.BankLocal;
import fina2.dcs.fis.BankType;
import fina2.dcs.fis.Criterion;
import fina2.dcs.fis.CriterionLocal;
import fina2.dcs.fis.Licence;
import fina2.dcs.fis.LicenceLocal;
import fina2.dcs.fis.LicenceType;
import fina2.dcs.fis.LicenceTypeLocal;
import fina2.dcs.fis.MMBankGroup;
import fina2.dcs.fis.MMBankGroupLocal;

@Name("fiListTreeController")
@Scope(ScopeType.SESSION)
public class FiListTreeController implements Serializable {
	private HtmlTree tree;
	private TreeState treeState;
	private String code = null;
	private String selectedCode = null;
	private BankType curBankType = null;
	private Bank curBank = null;
	private boolean btNoNodes = false;
	private boolean bNoNodes = false;

	private boolean rootSelected = true;
	private boolean bankTypeSelected = false;
	private boolean bankSelected = false;
	private boolean bankGroupLabelSelected = false;
	private boolean bankGroupSelected = false;
	private boolean licenceLabelSelected = false;
	private boolean licenceSelected = false;
	private boolean managementLabelSelected = false;
	private boolean managementSelected = false;
	private boolean historyLabelSelected = false;
	private boolean historySelected = false;
	private boolean branchLabelSelected = false;
	private boolean branchSelected = false;

	private InitialContext ic = null;
	private BankLocal bankLocal = null;
	private LicenceLocal licLocal = null;
	private BankGroupLocal bgLocal = null;
	private CriterionLocal critLocal = null;
	private LicenceTypeLocal licTypeLocal = null;
	private MMBankGroupLocal mmBGroupLocal = null;
	private List<Bank> initBankList = null;
	private ArrayList<BankGroup> bankGroups = null;
	private ArrayList<LicenceType> licences = null;
	private BigDecimal bankId = null;

	private Log log = Logging.getLog(getClass());

	public HtmlTree getTree() {
		return tree;
	}

	public void setTree(HtmlTree tree) {
		this.tree = tree;
	}

	public TreeState getTreeState() {
		return treeState;
	}

	public void setTreeState(TreeState treeState) {
		this.treeState = treeState;
	}

	public boolean isRootSelected() {
		return rootSelected;
	}

	public void setRootSelected(boolean rootSelected) {
		this.rootSelected = rootSelected;
	}

	public boolean isBankTypeSelected() {
		return bankTypeSelected;
	}

	public void setBankTypeSelected(boolean bankTypeSelected) {
		this.bankTypeSelected = bankTypeSelected;
	}

	public boolean isBankSelected() {
		return bankSelected;
	}

	public void setBankSelected(boolean bankSelected) {
		this.bankSelected = bankSelected;
	}

	public boolean isBankGroupLabelSelected() {
		return bankGroupLabelSelected;
	}

	public void setBankGroupLabelSelected(boolean bankGroupLabelSelected) {
		this.bankGroupLabelSelected = bankGroupLabelSelected;
	}

	public boolean isBankGroupSelected() {
		return bankGroupSelected;
	}

	public void setBankGroupSelected(boolean bankGroupSelected) {
		this.bankGroupSelected = bankGroupSelected;
	}

	public boolean isLicenceLabelSelected() {
		return licenceLabelSelected;
	}

	public void setLicenceLabelSelected(boolean licenceLabelSelected) {
		this.licenceLabelSelected = licenceLabelSelected;
	}

	public boolean isLicenceSelected() {
		return licenceSelected;
	}

	public void setLicenceSelected(boolean licenceSelected) {
		this.licenceSelected = licenceSelected;
	}

	public boolean isManagementLabelSelected() {
		return managementLabelSelected;
	}

	public void setManagementLabelSelected(boolean managementLabelSelected) {
		this.managementLabelSelected = managementLabelSelected;
	}

	public boolean isManagementSelected() {
		return managementSelected;
	}

	public void setManagementSelected(boolean managementSelected) {
		this.managementSelected = managementSelected;
	}

	public boolean isHistoryLabelSelected() {
		return historyLabelSelected;
	}

	public void setHistoryLabelSelected(boolean historyLabelSelected) {
		this.historyLabelSelected = historyLabelSelected;
	}

	public boolean isHistorySelected() {
		return historySelected;
	}

	public void setHistorySelected(boolean historySelected) {
		this.historySelected = historySelected;
	}

	public boolean isBranchLabelSelected() {
		return branchLabelSelected;
	}

	public void setBranchLabelSelected(boolean branchLabelSelected) {
		this.branchLabelSelected = branchLabelSelected;
	}

	public boolean isBranchSelected() {
		return branchSelected;
	}

	public void setBranchSelected(boolean branchSelected) {
		this.branchSelected = branchSelected;
	}

	public void expandAll() {
		try {
			if (tree != null)
				tree.queueExpandAll();
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
	}

	public void collapseAll() {
		try {
			if (tree != null)
				tree.queueCollapseAll();
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
	}

	public void initNodeCode(String code) {
		this.code = code;
	}

	public boolean adviseNodeSelected(UITree t) {

		boolean selected = false;
		if (selectedCode != null) {
			if (t.getRowData() instanceof BankType) {
				String bCode = ((BankType) t.getRowData()).getCode();
				if ((bCode != null) && (bCode.trim().equals(selectedCode.trim()))) {
					t.setSelected();
					selectedCode = null;
					selected = true;
				}
			}
		}
		if (code != null) {
			if (t.getRowData() instanceof Bank) {
				String bCode = ((Bank) t.getRowData()).getCode();
				if ((bCode != null) && bCode.trim().equals(code.trim())) {
					t.setSelected();
					selected = true;
					code = null;
				}
			}
		}
		return selected;
	}

	public boolean adviseNodeOpened(UITree t) {

		boolean opened = false;
		try {

			if (code != null) {
				if (t.getRowData() instanceof BankType) {
					bankLocal = ContextHelper.injectBankLocal(ic);
					curBankType = (BankType) t.getRowData();
					initBankList = bankLocal.getBanksByTypeId(curBankType.getId().toBigInteger().toString());
					curBankType.getBanks().clear();
					curBankType.getBanks().addAll(initBankList);
					if ((initBankList != null))
						for (int i = 0; i < initBankList.size(); i++) {
							if (initBankList.get(i).getCode().trim().equals(code.trim())) {
								opened = true;
								t.queueExpandAll();
								break;
							}
						}
				}
			} else if ((t.getRowData() instanceof BankType) && (curBankType != null) && (curBankType.equals((BankType) t.getRowData()))) {
				Object rowKey = tree.getRowKey();
				TreeRowKey key = (TreeRowKey) tree.getRowKey();
				TreeState state = (TreeState) tree.getComponentState();

				if (curBankType.getBanks().size() == 0) {
					opened = false;
					btNoNodes = true;
					t.queueCollapseAll();
				} else if (btNoNodes || state.isExpanded(key)) {
					t.queueExpandAll();
					opened = true;
					btNoNodes = false;
				} else {
					t.queueCollapseAll();
					opened = false;
				}
				curBankType = null;
			} else if ((t.getRowData() instanceof Bank) && (curBank != null) && (curBank.equals((Bank) t.getRowData()))) {
				Object rowKey = tree.getRowKey();
				TreeRowKey key = (TreeRowKey) tree.getRowKey();
				TreeState state = (TreeState) tree.getComponentState();
				if ((curBank != null) && (curBank.getBankGroups() != null) && (curBank.getBankGroups().size() == 0)) {
					opened = false;
					bNoNodes = true;
					t.queueCollapseAll();
				}
				if (bNoNodes || state.isExpanded(key)) {
					if (bNoNodes)
						bankGroups.clear();
					t.queueExpandAll();
					opened = true;
					bNoNodes = false;
				} else {
					t.queueCollapseAll();
					((Bank) t.getRowData()).setBankGroups(null);
					opened = false;
				}

			} else if ((t.getRowData() != null) && (t.getRowData().toString() != null) && (t.getRowData().toString().equals("BankGroups"))) {
				Object rowKey = tree.getRowKey();
				TreeRowKey key = (TreeRowKey) tree.getRowKey();
				TreeState state = (TreeState) tree.getComponentState();
				if (state.isExpanded(key)) {
					t.queueExpandAll();
					opened = true;
				} else {
					t.queueCollapseAll();
					opened = false;
				}

			} else if ((t.getRowData() != null) && (t.getRowData().toString() != null) && (t.getRowData().toString().equals("Licences"))) {
				Object rowKey = tree.getRowKey();
				TreeRowKey key = (TreeRowKey) tree.getRowKey();
				TreeState state = (TreeState) tree.getComponentState();
				if (state.isExpanded(key)) {
					t.queueExpandAll();
					opened = true;
				} else {
					t.queueCollapseAll();
					opened = false;
				}

			}
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return opened;
	}

	public void expandCollapseBankType(NodeExpandedEvent e) {

		HtmlTree btNodes = (HtmlTree) e.getSource();
		Object o = btNodes.getRowData();
		try {
			ic = ContextHelper.getInitialContext();

			if (o instanceof BankType) {
				bankLocal = ContextHelper.injectBankLocal(ic);
				curBankType = (BankType) o;
				initBankList = bankLocal.getBanksByTypeId(curBankType.getId().toBigInteger().toString());
				curBankType.getBanks().clear();
				curBankType.getBanks().addAll(initBankList);

			} else if (o instanceof Bank) {
				curBank = (Bank) o;
				bankId = curBank.getId();
				if (curBank.getBankGroups() != null)
					curBank.getBankGroups().clear();
				if (curBank.getLicenceTypes() != null)
					curBank.getLicenceTypes().clear();
				bankGroups = new ArrayList<BankGroup>();
				bankGroups.add(new BankGroup());
				licences = new ArrayList<LicenceType>();
				licences.add(new LicenceType());
				curBank.setBankGroups(bankGroups);
				curBank.setLicenceTypes(licences);
			} else if (o != null && o.toString().equals("BankGroups")) {
				if ((curBank != null) && (curBank.getBankGroups() != null))
					curBank.getBankGroups().clear();
				mmBGroupLocal = ContextHelper.injectMMBankGroupLocal(ic);
				bgLocal = ContextHelper.injectBankGroupLocal(ic);
				critLocal = ContextHelper.injectCriterionLocal(ic);
				bankGroups.clear();
				List<MMBankGroup> mmBankGroups = mmBGroupLocal.findByBankId(bankId.floatValue());
				if (mmBankGroups != null) {
					for (int ii = 0; ii < mmBankGroups.size(); ii++) {
						Float bgId = mmBankGroups.get(ii).getBankGroupPk().getBankGroupId();
						BankGroup b = bgLocal.getBankGroupById(new BigDecimal(bgId));
						BigDecimal critId = b.getCriterionId();
						Criterion c = critLocal.findById(critId);
						b.setParentDesc(c.getDescription());
						bankGroups.add(b);
					}
				}
				curBank.setBankGroups(bankGroups);
			} else if (o != null && o.toString().trim().equals("Licences")) {
				if ((curBank != null) && (curBank.getLicenceTypes() != null))
					curBank.getLicenceTypes().clear();
				licences.clear();
				licLocal = ContextHelper.injectLicenceLocal(ic);
				licTypeLocal = ContextHelper.injectLicenceTypeLocal(ic);
				List<Licence> lics = licLocal.findByBankId(bankId);
				if (lics != null) {
					for (int k = 0; k < lics.size(); k++) {
						LicenceType lic = licTypeLocal.findById(lics.get(k).getTypeId());
						licences.add(lic);
					}
				}
				curBank.setLicenceTypes(licences);
			}

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
	}

	public static void main(String[] args) {
		try {
			File f = new File("C:\\fina-client\\gui\\lib\\jboss_client");
			String s[] = f.list();
			for (int i = 0; i < s.length; i++) {
				System.out.println("<jar href=" + '"' + s[i] + '"' + "/>");

			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
